library(tidyverse)
library(foreign)
library(lubridate)
library(scales)

rm(list=ls())

####################################################################
# Figure A.1: Reported and Observed Turnout in Referendums, 1981-2009
#####################################################################


# (i) Read in VOX data
data <- read.dta("data3.dta") %>%  drop_na(year,month, day)

df1 <- data %>%  
       group_by(year,month, day) %>% 
       dplyr::summarize(turnout_vox = mean(turnout,na.rm=TRUE)*100) 

# (ii) Read in official turnout data

data2 <- read.table("data10.csv",sep=";",header = TRUE)
head(data2)
help <- strsplit(as.character(data2$Datum_Vorlage), ".", fixed = TRUE)

data2$turnout<- as.numeric(data2$Beteiligung)

df2 <- data2 %>%  
  group_by(year,month, day) %>% 
  dplyr::summarize(turnout_official = mean(turnout,na.rm=TRUE)) 

df_all <- df1 %>% inner_join(df2, by = c("day","month","year"))

df_all_long <- df_all %>% gather(category, turnout, c("turnout_vox","turnout_official"))

df_all_long <- df_all_long[order(df_all_long$year,df_all_long$month,df_all_long$day),]
df_all_long$event_id <- c(1:length(df_all_long$year))
xbreaks= c(1,17,41,73,99,133,155)
xlabel=c(1981,seq(1985,2005,5),2009)

ggplot(df_all_long, aes(y = turnout/100,x = event_id, shape=factor(category), linetype=factor(category))) +
  geom_point(size=2.8) + 
  geom_line(size=1.1) + 
  scale_shape_manual(limits=c("turnout_vox","turnout_official"),values=c(2,16), labels = c("Turnout in Survey  ", "Official Turnout")) +
  scale_linetype_manual(limits=c("turnout_vox","turnout_official"),values=c(3,1), labels = c("Turnout in Survey  ", "Official Turnout")) +
  theme_bw(base_size = 42)  + 
  labs(linetype="",shape="") +
  ylab("\n Turnout \n") + xlab("Referendum day") +
  scale_y_continuous(labels=percent) +
  scale_x_continuous(breaks=xbreaks,labels=xlabel) +
  theme(legend.position="bottom")

ggsave(file="FigureA1.pdf",width=20,height=10 )



